package com.baizhi.service.impl;

import com.baizhi.dao.LogDao;
import com.baizhi.entity.Log;
import com.baizhi.service.LogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 野马 on 2019/6/19.
 */

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogDao logDao;

    private Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);

    @Override
    public Map insertLog(Log log) {
        Map map = new HashMap();
        try {
            logDao.insertLog(log);
            map.put("loginsert",true);
        } catch (Exception e) {
            map.put("loginsert",false);
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map selectLog(String username, int page, int rows) {
        int start = (page-1)*rows;
        int pageSize = rows;

        if(username != null && !"".equals(username)){
            username = "%"+username+"%";
        }else {
            username = null;
        }
        int countLog = logDao.countLog();
        List<Log> logList = logDao.selectLog(username, start, pageSize);

        logger.info(""+logList+"日志的信息");
        Map map = new HashMap();
        map.put("total",countLog);
        map.put("rows",logList);


        return map;
    }

    @Override
    public Map deletepl(int[] logIds) {
        Map map = new HashMap();
        try {
            logDao.deletepl(logIds);
            map.put("logdeletepl",true);
        } catch (Exception e) {
            map.put("logdeletepl",false);
            e.printStackTrace();
        }
        return map;
    }
}
